// ==UserScript==
// @name         轻书架切换登录
// @namespace    Schwi
// @version      1.6
// @description  轻书架账号切换，欢迎反馈和优化
// @author       Schwi
// @match        https://www.acgdmzy.com/*
// @match        https://www.lightnovel.app/*
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

console.log('这里是脚本的开头');

(() => {
    'use strict';
    let t = setInterval(() => {
        let h = document.querySelector("#app > div > div:nth-child(1) > header > div > div.v-avatar.ml-1");
        console.log('找啊找~');
        if (h != null) { // 头像元素存在,说明加载完成
            clearInterval(t); // 清除定时器
            loadcomplete(); // 调用回调函数
        }
    }, 1000);
})();

function loadcomplete() {
    console.log('加载完成');
    console.log('更新点token');
    try {
        let app = document.querySelector('#app').__vue__;
        let store = app.$store;
        let account = store.getters.userData.user_name;
        let token = store.getters.token;
        GM_setValue(account, token);
    }
    catch (err) {
        console.log('报错啦！' + err);
    }
    console.log('设计点元素');
    let a = document.querySelector("#app > div > div:nth-child(1) > header > div");
    let div1 = document.createElement('div');
    let div2 = document.createElement('div');
    let br = document.createElement('br');
    let btn1 = document.createElement('button');
    btn1.textContent = '切换账号';
    btn1.style.color = '#fff'
    btn1.style.border = "1";
    btn1.id = 'Multiple-accounts';
    let btn2 = document.createElement('button');
    btn2.textContent = '保存账号';
    btn2.style.color = '#fff'
    btn2.style.border = "1";
    btn2.id = 'Multiple-accounts';
    let ul1 = document.createElement('ul');
    let select = document.createElement('select');
    select.id = 'Dropdown';
    select.style.border = "1";
    let value1 = document.createElement('option');
    let value2 = document.createElement('option');
    let value3 = document.createElement('option');
    value1.textContent = '账号1';
    value2.textContent = '账号2';
    value3.textContent = '账号3';
    value1.value = '1';
    value2.value = '2';
    value3.value = '3';
    value1.id = 'Dropdown1';
    value2.id = 'Dropdown2';
    value3.id = 'Dropdown3';
    btn1.addEventListener('click', () => {
        let Dropdown = Number(document.getElementById("Dropdown").value);
        if (GM_getValue(Dropdown)) {
            let account = GM_getValue(Dropdown)
            let token = GM_getValue(account)
            localStorage.setItem('token', token)
            window.location.reload();
        } else {
            alert('未保存账号')
        }
    });
    btn2.addEventListener('click', () => {
        try {
            let Dropdown = Number(document.getElementById("Dropdown").value);
            let app = document.querySelector('#app').__vue__;
            let store = app.$store;
            let account = store.getters.userData.user_name;
            let token = store.getters.token;
            GM_setValue(Dropdown, account);
            GM_setValue(account, token);
            document.getElementById("Dropdown" + Dropdown).innerHTML = account;
        }
        catch (err) {
            console.log('报错啦！' + err);
            alert('报错啦！' + err);
        }
    });
    console.log('加点元素');
    a.appendChild(div1);
    a.appendChild(div2);
    div1.appendChild(ul1);
    ul1.appendChild(select);
    select.appendChild(value1);
    select.appendChild(value2);
    select.appendChild(value3);
    div2.appendChild(btn1);
    div2.appendChild(br);
    div2.appendChild(btn2);
    console.log('改点东西');
    if (GM_getValue('1')) {
        document.getElementById("Dropdown1").innerHTML = GM_getValue('1');
    }
    if (GM_getValue('2')) {
        document.getElementById("Dropdown2").innerHTML = GM_getValue('2');
    }
    if (GM_getValue('3')) {
        document.getElementById("Dropdown3").innerHTML = GM_getValue('3');
    }
}
